package com.edou.auth.tag;

import java.util.List;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.lang3.StringUtils;

import com.edou.auth.entity.Right;
import com.edou.base.constant.SessionConstant;

/**
 * 权限标签，判断当前用户是否有该操作权限
 * @creator     admin
 * @create-time 2017年3月3日   下午2:27:00
 * @email admin@edou.com
 * @company www.edou.com
 * @version 1.0
 */
public class AuthTag extends BodyTagSupport{
	private static final long serialVersionUID = -8314365112353913892L;
	
	/** 权限代码 */
	private String pcode;

	@SuppressWarnings("unchecked")
	@Override
	public int doStartTag() throws JspException {
		//从session里面获取当前用户的权限数据
		HttpSession session = pageContext.getSession();
		List<Right> rights = (List<Right>) session.getAttribute(SessionConstant.CURRENT_PRIVILEGE);
		
		if(StringUtils.isNotBlank(pcode)){
			for(Right curRight : rights){
				if(StringUtils.equals(curRight.getPcode(), pcode)){
					return EVAL_BODY_INCLUDE;
				}
			}
		}
		
		return SKIP_BODY;
	}

	/** 权限代码 */
	public String getPcode() {
		return pcode;
	}

	/** 权限代码 */
	public void setPcode(String pcode) {
		this.pcode = pcode;
	}
	
}
